<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org/">
<head>
    <title>修改密码</title>
    <link rel="stylesheet" th:href="@{/static/layui/css/layui.css}">
    <script th:src="@{/static/jquery-2.1.4.js}" type="text/javascript" charset="utf-8"></script>
    <script th:src="@{/static/layui/layui.js}" type="text/javascript" charset="utf-8"></script>
    <script th:src="@{/static/mylayer.js}" type="text/javascript" charset="utf-8"></script>
    <script th:src="@{/static/kindeditor/kindeditor.js}" type="text/javascript" charset="utf-8"></script>
</head>
<body>
<style>
    .demo-reg-container{width: 320px; margin: 21px auto 0;}
    .demo-reg-other .layui-icon{position: relative; display: inline-block; margin: 0 2px; top: 2px; font-size: 26px;}
</style>
<form class="layui-form" lay-filter="updateFormFilter" action="">
    <div class="demo-reg-container">
        <input type="hidden" name="id"> <!--隐藏id-->

        原密码：
        <div class="layui-form-item">
            <div class="layui-input-wrap">
                <div class="layui-input-prefix">
                    <i class="layui-icon layui-icon-password"></i>
                </div>
                <input type="password" name="oldPassword" value="" lay-verify="required|oldPass" placeholder="" autocomplete="off" class="layui-input" id="oldPassword" lay-affix="eye">
            </div>
        </div>
        新密码：
        <div class="layui-form-item">
            <div class="layui-input-wrap">
                <div class="layui-input-prefix">
                    <i class="layui-icon layui-icon-password"></i>
                </div>
                <input type="password" name="newPassword" value="" lay-verify="required|newPass" placeholder="" autocomplete="off" class="layui-input" id="newPassword" lay-affix="eye">
            </div>
        </div>
        确认密码：
        <div class="layui-form-item">
            <div class="layui-input-wrap">
                <div class="layui-input-prefix">
                    <i class="layui-icon layui-icon-password"></i>
                </div>
                <input type="password" name="confirmPassword" value="" lay-verify="required|confirmPassword" placeholder="" autocomplete="off" class="layui-input" lay-affix="eye">
            </div>
        </div>

        <div class="layui-form-item">
            <button class="layui-btn layui-btn-fluid" lay-submit lay-filter="submit">修改密码</button>
        </div>
    </div>
</form>

<script>
    layui.use(function(){
        var $ = layui.$;
        var form = layui.form;
        var layer = layui.layer;

        //拿到index.jsp传递过来的参数--编辑回显数据
        var queryString = window.location.search;
        var urlParams = new URLSearchParams(queryString);
        var id = urlParams.get("id");

        $.post(
            '/admin/selectById',
            {'id':id},
            function (result) {
                //console.log(result);
                if (result.code == 0){
                    form.val('updateFormFilter',result.data);
                }
            },
            'json'
        );

        // 自定义验证规则
        form.verify({
            //oldPass
            oldPass:function (value,elem) {
                if (!/^[\S]{3,9}$/.test(value)){
                    return '密码必须为3到9位的非空字符串';
                }

            },
            newPass:function (value,elem) {
                if (!/^[\S]{3,9}$/.test(value)){
                    return '密码必须为3到9位的非空字符串';
                }
            },
            // 确认密码
            confirmPassword: function(value, item){
                var passwordValue = $('#newPassword').val();
                if(value !== passwordValue){
                    return '两次密码输入不一致';
                }
            }
        });

        //绑定提交事件
        form.on('submit(submit)',function (data) {
            //获取表单字段
            var field = data.field;
            console.log(data);
            // 此处可执行 Ajax 等操作
            $.post(
                '/admin/updatePass',
                field,
                function (result) {
                    console.log(result);
                    if (result.code == 0) {
                        mylayer.okMsg(result.msg);
                        setInterval(function (){
                            parent.location.href = '/page/login';
                        },2000)
                    } else {
                        mylayer.errorMsg(result.msg);
                    }
                },
                'json'
            );
            return false;//阻止默认form跳转
        });
    });

</script>

</body>
</html>